// pages/artificer/order/order.js
var api = require('../../../api/api');
Page({

  /**
   * 页面的初始数据
   */
  data: {
      tab:[
        {name:'全部',selected:true,state:-1},
        {name:'待支付',selected:false,state:0},
        {name:'待发货',selected:false,state:1},
        {name:'待收货',selected:false,state:2},
        {name:'已完成',selected:false,state:3},
        // {name:'已评价',selected:false,state:4},
        {name:'已退款',selected:false,state:5},
      ],
      page_begin:'',
      lists:[],
      page:1,
      lastPage:0,
      state:-1
  },
  changeTab(e){
    let index = e.currentTarget.dataset.index;
    let tab = this.data.tab;
    let state = '';
    tab.forEach((item,key) => {
      if(key == index){
        item.selected = true;
        state = item.state;
      }else{
        item.selected =  false;
      }
    });
    this.setData({
      tab,
      state,
      lists:[],
      page:1,
      lastPage:0
    })
    this.getOrderLists();
  },
  getOrderLists(){
    if(this.data.lastPage!=0 && this.data.page  >= this.data.lastPage){
      wx.showToast({
        title: '没有更多了',
        icon:'none'
      })
      return false;
    }
    api.get('/order/list',{
      state:this.data.state,
      page:this.data.page,
    }).then(res=>{
      this.setData({
        lists:res.items,
        lastPage:res.lastPage,
        page:this.data.page + 1
      })
  })
  },
  goToDetial(e){
    let index = e.currentTarget.dataset.index;
    let orderId = this.data.lists[index].order_no
    wx.navigateTo({
      url: '/pages/shop_order/order_details/order_details?no='+orderId,
    })
  },
  tabUrl(e){
    let that = this;
    let index = e.currentTarget.dataset.index;
    let url = that.data.menu[index].url;
    wx.redirectTo({
      url: url,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getOrderLists();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    //获得dialog组件

  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})